package com.nanbei.eduService.controller;


import com.nanbei.commonUntil.R;
import com.nanbei.eduService.client.VodClient;
import com.nanbei.eduService.entity.EduVideo;
import com.nanbei.eduService.service.EduVideoService;
import com.nanbei.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/eduService/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    /**
     * 添加小节信息
     * @param eduVideo
     * @return
     */
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return  R.ok();
    }

    /**
     * 根据ID查询小节信息做数据回显
     * @param videoId
     * @return
     */
    @GetMapping("/findVideoById/{videoId}")
    public R findVideoById(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return  R.ok().data("eduVideo",eduVideo);
    }

    /**
     * 更新小节信息
     * @param eduVideo
     * @return
     */
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    /**
     * 根据ID删除小节信息  TODO 后面需要先删除视频在删除小节
     * @param videoId
     * @return
     */
    @DeleteMapping("delVideoById/{videoId}")
    public R delVideoById(@PathVariable String videoId){

        //通过小节ID查询视频ID
        EduVideo eduVideo = eduVideoService.getById(videoId);
        //通过视频ID对视频进行删除
        String id = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(id)){
            R r = vodClient.delVideoToALi(id);
            if (r.getCode() ==20001 ){
                throw new GuliException(20001,"视频服务器连接超时");
            }
        }

        //删除视频后删除小节信息
        eduVideoService.removeById(videoId);
        return R.ok();
    }
}

